package com.wushi.lenovo.asia5b.app_ui.good.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mvp.images.WImageLoader;
import com.mvp.utils.ResString;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.goodbean.OrderGoodBean;
import com.wushi.lenovo.asia5b.app_model.good.orderitem.OrderItemType;
import com.wushi.lenovo.asia5b.app_ui.good.listener.ButtonListener;
import com.wushi.lenovo.asia5b.app_ui.good.listener.OrderAdapterListener;
import com.wushi.lenovo.asia5b.app_widget.mingan.MinganImageView;

import java.util.List;

/**
 * Created by zhangyuncai on 2020/3/3.
 * 订单列表中的商品列表
 */
public class OrderGoodsAdapter extends BaseQuickAdapter<OrderGoodBean, BaseViewHolder> {
    final OrderItemType type;
    ButtonListener listener;
    final OrderAdapterListener orderAdapterListener;

    public static OrderGoodsAdapter newInstance(OrderItemType type, @Nullable List<OrderGoodBean> data, OrderAdapterListener orderAdapterListener) {
        if (type == OrderItemType.Verification) {
            return new OrderGoodsAdapter(type, R.layout.item_goodsadapter_order_verifi, data, orderAdapterListener);
        } else if (type == OrderItemType.Payment) {
            return new OrderGoodsAdapter(type, R.layout.item_goodsadapter_order_payment, data, orderAdapterListener);
        } else if (type == OrderItemType.Problematic) {
            return new OrderGoodsAdapter(type, R.layout.item_goodsadapter_order_problem, data, orderAdapterListener);
        }
        return null;
    }

    public void setButtoListener(ButtonListener buttoListener) {
        this.listener = buttoListener;
    }

    public OrderGoodsAdapter(OrderItemType type, int layoutResId, @Nullable List<OrderGoodBean> data, OrderAdapterListener orderAdapterListener) {
        super(layoutResId, data);
        this.type = type;
        this.orderAdapterListener = orderAdapterListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderGoodBean item) {
        if (type == OrderItemType.Verification) {
            setGoodsViewVerification(helper, item);
        } else if (type == OrderItemType.Payment) {
            setGoodsViewPayment(helper, item);
        } else if (type == OrderItemType.Problematic) {
            setGoodsViewProblem(helper, item);
        }
    }

    /**
     * 处理中
     *
     * @param helper
     * @param item
     */
    public void setGoodsViewVerification(BaseViewHolder helper, OrderGoodBean item) {

        //商品详情
        setGoodsView(helper, item);

        //复选框
        ImageView iv_order_check = helper.getView(R.id.iv_order_check);
        if (item.getPay_type() == 3) {//可以打包
            if (item.isChecked) {
                if (item.isMingan()) {
                    iv_order_check.setImageResource(R.drawable.ic_payment_s);
                } else {
                    iv_order_check.setImageResource(R.drawable.ic_check_press);
                }
            } else {
                iv_order_check.setImageResource(R.drawable.ic_check_normal);
            }
            iv_order_check.setOnClickListener(v -> {
                item.isChecked = !item.isChecked;
                orderAdapterListener.selectGoods(item);
                notifyItemChanged(helper.getLayoutPosition());
            });
        } else {
            iv_order_check.setOnClickListener(v -> {
            });
            iv_order_check.setImageResource(R.drawable.ic_order_unclickcheck);
        }


    }

    /**
     * 付款
     *
     * @param helper
     * @param item
     */
    public void setGoodsViewPayment(BaseViewHolder helper, OrderGoodBean item) {

        ///商品详情
        setGoodsView(helper, item);

        //订单按钮
        setButtonListView(helper, item);
        boolean hasAction = (item.getAction() != null) && (item.getAction().size() != 0);
        helper.setGone(R.id.ll_order_diffpay_vessel, hasAction);

        helper.setText(R.id.tv_order_goodstotal,item.getDiff_total());
    }

    /**
     * 历史
     *
     * @param helper
     * @param item
     */
    public void setGoodsViewProblem(BaseViewHolder helper, OrderGoodBean item) {
        //商品详情
        setGoodsView(helper, item);

        //订单按钮
        setButtonListView(helper, item);


    }

    public void setGoodsView(BaseViewHolder helper, OrderGoodBean item) {
        //商品图片
        MinganImageView mingan_orderitem = helper.getView(R.id.mingan_order);
        WImageLoader.displayImage(mContext, item.getImage(), mingan_orderitem.getImageView());
        //是否显示敏感文字
        mingan_orderitem.setMinganTextVisibility(item.isMingan());

        //订单状态
        setOrderStateText(helper, item);

        //商品名称
        TextView tv_order_goodname = helper.getView(R.id.tv_order_goodname);
        tv_order_goodname.setText(item.getProduct_name());

        //包裹编号
        TextView tv_order_goodno = helper.getView(R.id.tv_order_goodno);
        tv_order_goodno.setText(String.format("%s:%d",ResString.get(R.string.parcelno), item.getId()));

        //价格+数量
        TextView tv_order_priceandnum = helper.getView(R.id.tv_order_priceandnum);
        tv_order_priceandnum.setText(String.format(ResString.get(R.string.price_nun), item.unit_price, item.getQuantity()));

        //价格详情按钮
        helper.getView(R.id.view_goods_pricedetail).setOnClickListener(v -> {
            orderAdapterListener.priceDetail(item);
        });

        //商品详情
        helper.getView(R.id.mingan_order).setOnClickListener(v -> {
            orderAdapterListener.goodsDetail(item);
        });
        helper.getView(R.id.tv_order_goodname).setOnClickListener(v -> {
            orderAdapterListener.goodsDetail(item);
        });
    }

    public void setOrderStateText(BaseViewHolder helper, OrderGoodBean item) {
        //商品订单状态
        TextView tv_order_type = helper.getView(R.id.tv_order_type);
        tv_order_type.setText(String.format("%s", item.getStatus_name()));
        if (type == OrderItemType.Problematic) {
            //设置问题按钮左右icon样式
//            Drawable left = ContextCompat.getDrawable(mContext, R.drawable.ic_orderitem_info_white);
//            left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
//            Drawable right = ContextCompat.getDrawable(mContext, R.drawable.ic_orderitem_arrow_white);
//            right.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
//            tv_order_type.setCompoundDrawables(left,
//                    null,
//                    right,
//                    null);

            //添加监听
            tv_order_type.setOnClickListener(v -> {
                if (listener != null) {
                    listener.problemClick(item);
                }
            });
        }

        //设置包裹状态背景颜色
        tv_order_type.setBackgroundColor(item.getStatus_color(mContext));

    }

    /**
     * 设置底部按键列表
     *
     * @param helper
     * @param item   商品数据
     */
    private void setButtonListView(BaseViewHolder helper, OrderGoodBean item) {
        int position = helper.getLayoutPosition();
        //button列表
        RecyclerView recyclerView = helper.getView(R.id.recyclerview_order_button);
        List<OrderGoodBean.ActionBean> buttonList = item.getAction();
        if (buttonList == null || buttonList.size() == 0) {
            recyclerView.setVisibility(View.GONE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            ButtonAdapter adapter = new ButtonAdapter(buttonList);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            recyclerView.setAdapter(adapter);
            adapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position2) {
                    OrderGoodBean.ActionBean actionBean = (OrderGoodBean.ActionBean) adapter.getItem(position2);
                    if (listener != null && actionBean.getEnabled() == 1) {
                        listener.actionClick(actionBean, null,item);
                    }
                }
            });
        }
    }

}
